package com.hulk.dryad.admin.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.hulk.dryad.admin.api.vo.SysTokenVO;
import com.hulk.dryad.admin.mapstruct.SysTokenStruct;
import com.hulk.dryad.admin.service.SysUserService;
import com.hulk.dryad.common.annotation.OperLog;
import com.hulk.dryad.common.annotation.PrintlnLog;
import com.hulk.dryad.common.component.R;
import com.hulk.dryad.common.constant.enums.*;
import com.hulk.dryad.persistence.entity.SysUserModel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 认证 token 管理
 *
 * @author hulk
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/admin/token")
@Api(value = "token", tags = "令牌管理")
public class SysTokenController {

	private final SysUserService userService;

	@PrintlnLog
	@ApiOperation(value = "tokenbyname", notes = "根据用户名查询令牌信息")
	@GetMapping("/token/{username}")
	public R getToken( String username) {
		SysUserModel model =
				userService.getOne(
						Wrappers.<SysUserModel>query()
								.lambda()
								.eq(SysUserModel::getUsername, username)
								.eq(SysUserModel::getDelFlag, DelFlag.N.getValue())
								.eq(SysUserModel::getLockFlag, LockFlag.N.getValue())
								.eq(SysUserModel::getOnlineFlag, OnlineFlag.Y.getValue()));
		if (model == null) {
			return R.failed(BEC.E_2015, String.format("用户信息为空 %s", username));
		}
		SysTokenVO vo = SysTokenStruct.INSTANCE.from(model);
		return R.ok(vo);
	}
	/**
	 * 分页查询TOKEN
	 * @return 用户集合
	 */
	@ApiOperation(value = "getTokenPage", notes = "分页查询令牌")
	@ApiOperationSupport(order = 6)
	@PrintlnLog
	@GetMapping("/page")
	public R getTokenPage(Page page) {
		LambdaQueryWrapper<SysUserModel> qw = Wrappers.<SysUserModel>lambdaQuery()
				.eq(SysUserModel::getDelFlag, DelFlag.N.getValue())
				.eq(SysUserModel::getLockFlag, LockFlag.N.getValue())
				.eq(SysUserModel::getOnlineFlag, OnlineFlag.Y.getValue());
		Page rpgm =	userService.page(page,qw);
		List<SysUserModel> models = rpgm.getRecords();
		List<SysTokenVO> vos = SysTokenStruct.INSTANCE.from(models);
		rpgm.setRecords(vos);
		return R.ok(rpgm);
	}
	@OperLog(value ="删除令牌踢出用户",action = ActionType.DEL)
	@ApiOperation(value = "removeByIds", notes = "删除令牌踢出用户")
	@PostMapping("/kickout")
	@PreAuthorize("@pms.hasRoot()")
	public R removeByIds(@NotEmpty @RequestBody Set<Long> ids) {
	 List models =	ids.stream().map(v->{
		    SysUserModel   model  =new SysUserModel();
			model.setUserId(v);
		    model.setToken(StrUtil.uuid());
			model.setOnlineFlag(OnlineFlag.N.getValue());
			return model;
		}).collect(Collectors.toList());
		userService.updateBatchById(models);
		return R.ok();
	}

}
